package github.kawaiior.yuanchuxiuzhen.common.item.weapon.refine;

import github.kawaiior.yuanchuxiuzhen.common.block.fire.StrangeFire;
import github.kawaiior.yuanchuxiuzhen.common.item.fire.StrangeFireItem;
import github.kawaiior.yuanchuxiuzhen.util.MathUtil;
import net.minecraft.block.AbstractFireBlock;
import net.minecraft.block.BlockState;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.ActionResult;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.World;

import javax.annotation.Nullable;
import java.util.List;

public class FireGourd extends RefineWeaponBase{

    @Override
    public float getAttackDamage(){
        return 2F;
    }

    private void harvestFire(ItemStack stack){
        CompoundNBT nbt = stack.getOrCreateTag();
        int nbtLevel = nbt.getInt("fire");
        nbt.putInt("fire", nbtLevel+ 1);
    }

    @Override
    public ActionResultType onItemUse(ItemUseContext context) {
        World world = context.getWorld();
        BlockPos pos = context.getPos();
        BlockState blockState = world.getBlockState(pos);
        if (blockState.getBlock() instanceof StrangeFire){
            ItemEntity fireEntity = new ItemEntity(EntityType.ITEM, world);
            ItemStack fireItem;
            if (((StrangeFire) blockState.getBlock()).isYang()){
                fireItem = new ItemStack(StrangeFireItem.getYangStrangeFire(((StrangeFire) blockState.getBlock()).getLevel()));
            }
            else {
                fireItem = new ItemStack(StrangeFireItem.getYinStrangeFire(((StrangeFire) blockState.getBlock()).getLevel()));
            }
            fireEntity.setItem(fireItem);
            fireEntity.setPosition(pos.getX(), pos.getY() + 0.5D, pos.getZ());
            world.addEntity(fireEntity);

            world.destroyBlock(pos, true);
        }
        else if (blockState.getBlock() instanceof AbstractFireBlock){
            this.harvestFire(context.getItem());
            world.destroyBlock(pos, true);
        }
        return super.onItemUse(context);
    }

    public ItemStack getFire(int level){
        if (MathUtil.checkProbability(0.5F)){
            return new ItemStack(StrangeFireItem.getYangStrangeFire(level));
        }
        return new ItemStack(StrangeFireItem.getYinStrangeFire(level));
    }

    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        // shift+右键 将收集的能量转化为异火
        if (worldIn.isRemote){
            return super.onItemRightClick(worldIn, playerIn, handIn);
        }
        if (playerIn.isSneaking()){
            CompoundNBT nbt = playerIn.getHeldItem(handIn).getOrCreateTag();
            int nbtLevel = nbt.getInt("fire");
            int level = Math.min((nbtLevel / 10), 9);
            if (level > 0){
                nbt.putInt("fire", nbtLevel-level*10);
                playerIn.addItemStackToInventory(getFire(level));
            }
        }
        return super.onItemRightClick(worldIn, playerIn, handIn);
    }

    @Override
    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        CompoundNBT nbt = stack.getOrCreateTag();
        tooltip.add(new StringTextComponent("已收集"+nbt.getInt("fire")+"点能量"));
        tooltip.add(new StringTextComponent("shift+右键将能量转化为异火"));
    }
}
